package com.gljx.web.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.MakeupCharge;
import com.gljx.web.Entity.MakeupChargeEntry;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.MakeupChargeEntryMapper;
import com.gljx.web.dao.MakeupChargeMapper;
import com.gljx.web.service.MakeupChargeService;

@Transactional
@Service
public class MakeupChargeServiceImpl extends CommonDao implements MakeupChargeService{

	@Autowired
	private MakeupChargeMapper MakeupChargeMapper;

	@Autowired
	private MakeupChargeEntryMapper makeupChargeEntryMapper;

	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("Pindex", pindex);
		paramMap.put("Psql", psql);
		paramMap.put("Psize", psize);
		paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
		paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.MakeupChargeMapper.pagemap", paramMap, null);
		return  paramMap;
	}

	/**
	  取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @param Pcount 记录总数
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("Psql", psql);
		paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
		this.getSqlSession().select("com.gljx.web.dao.MakeupChargeMapper.pagecount", paramMap, null);
		return paramMap;
	}

	/**
	 * 实现添加
	 */
	@Override
	public void addMakeupCharge(MakeupCharge makeupCharge, String[] fseq, String[] cfstudentno, String[] cfstudentid,
			String[] cfidcardno, String[] cfidcardaddress, String[] cfsqcx, String[] cfstate, String[] cfkscs,
			String[] cfqkcs, String[] cfysje, String[] cfcash, String[] cfcard, String[] cfsjje, String[] cfremark) {
		// TODO Auto-generated method stub
		//添加父类信息
		MakeupChargeMapper.insertSelective(makeupCharge);
		if(fseq == null || fseq.length == 0){

		}else{
			for (int i = 0; i < fseq.length; i++) {
				MakeupChargeEntry makeupChargeEntry = new MakeupChargeEntry();
				makeupChargeEntry.setCfstudentno(cfstudentno[i]);
				makeupChargeEntry.setCfstudentid(cfstudentid[i]);
				makeupChargeEntry.setCfidcardno(cfidcardno[i]);
				makeupChargeEntry.setCfidcardaddress(cfidcardaddress[i]);
				makeupChargeEntry.setCfsqcx(cfsqcx[i]);
				makeupChargeEntry.setCfstate(Long.parseLong(cfstate[i]));
				if ("".equals(cfkscs[i])) {

				}else{
					makeupChargeEntry.setCfkscs(Long.parseLong(cfkscs[i]));
				}
				if ("".equals(cfqkcs[i])) {

				}else{
					makeupChargeEntry.setCfqkcs(Long.parseLong(cfqkcs[i]));
				}
				if ("".equals(cfysje[i])) {

				}else{
					makeupChargeEntry.setCfysje(BigDecimalFormat.formot(cfysje[i]));
				}
				if ("".equals(cfcash[i])) {

				}else{
					makeupChargeEntry.setCfcash(BigDecimalFormat.formot(cfcash[i]));
				}
				if ("".equals(cfcard[i])) {

				}else{
					makeupChargeEntry.setCfcard(BigDecimalFormat.formot(cfcard[i]));
				}
				if ("".equals(cfsjje[i])) {

				}else{
					makeupChargeEntry.setCfsjje(BigDecimalFormat.formot(cfsjje[i]));
				}
				makeupChargeEntry.setCfremark(cfremark[i]);
				makeupChargeEntry.setFid(GetFid.UUIDIntoFid());
				makeupChargeEntry.setFparentid(makeupCharge.getFid());
				makeupChargeEntry.setFseq((long) i+1);
				makeupChargeEntryMapper.insertSelective(makeupChargeEntry);
			}
		}
	}

	/**
	 * 实现删除
	 */
	@Override
	public void deleteMakeupChargeById(String[] deleteId, String fid) {
		// TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				//先删除子类
				makeupChargeEntryMapper.deleteByFparentId(deleteId[i]);
				//删除父类
				MakeupChargeMapper.deleteByPrimaryKey(deleteId[i]);
			}
		}else{
			//先删除子类
			makeupChargeEntryMapper.deleteByFparentId(fid);
			//删除父类
			MakeupChargeMapper.deleteByPrimaryKey(fid);
		}
	}

	/**
	 * 实现根据fid查询
	 */
	@Override
	public MakeupCharge queryMakeupChargeByFid(String fid) {
		// TODO Auto-generated method stub
		return MakeupChargeMapper.selectMakeupByFid(fid);
	}

	/**
	 * 实现修改
	 */
	@Override
	public void modifyMakeupCharge(MakeupCharge makeupCharge, String[] fseq, String[] cfstudentno, String[] cfstudentid,
			String[] cfidcardno, String[] cfidcardaddress, String[] cfsqcx, String[] cfstate, String[] cfkscs,
			String[] cfqkcs, String[] cfysje, String[] cfcash, String[] cfcard, String[] cfsjje, String[] cfremark) {
		// TODO Auto-generated method stub
		//修改父类
		MakeupChargeMapper.updateByPrimaryKeySelective(makeupCharge);
		//先删除子类
		makeupChargeEntryMapper.deleteByFparentId(makeupCharge.getFid());
		//在添加子类
		if(fseq == null || fseq.length == 0){

		}else{
			for (int i = 0; i < fseq.length; i++) {
				MakeupChargeEntry makeupChargeEntry = new MakeupChargeEntry();
				makeupChargeEntry.setCfstudentno(cfstudentno[i]);
				makeupChargeEntry.setCfstudentid(cfstudentid[i]);
				makeupChargeEntry.setCfidcardno(cfidcardno[i]);
				makeupChargeEntry.setCfidcardaddress(cfidcardaddress[i]);
				makeupChargeEntry.setCfsqcx(cfsqcx[i]);
				makeupChargeEntry.setCfstate(Long.parseLong(cfstate[i]));
				if ("".equals(cfkscs[i])) {

				}else{
					makeupChargeEntry.setCfkscs(Long.parseLong(cfkscs[i]));
				}
				if ("".equals(cfqkcs[i])) {

				}else{
					makeupChargeEntry.setCfqkcs(Long.parseLong(cfqkcs[i]));
				}
				if ("".equals(cfysje[i])) {

				}else{
					makeupChargeEntry.setCfysje(BigDecimalFormat.formot(cfysje[i]));
				}
				if ("".equals(cfcash[i])) {

				}else{
					makeupChargeEntry.setCfcash(BigDecimalFormat.formot(cfcash[i]));
				}
				if ("".equals(cfcard[i])) {

				}else{
					makeupChargeEntry.setCfcard(BigDecimalFormat.formot(cfcard[i]));
				}
				if ("".equals(cfsjje[i])) {

				}else{
					makeupChargeEntry.setCfsjje(BigDecimalFormat.formot(cfsjje[i]));
				}
				makeupChargeEntry.setCfremark(cfremark[i]);
				makeupChargeEntry.setFid(GetFid.UUIDIntoFid());
				makeupChargeEntry.setFparentid(makeupCharge.getFid());
				makeupChargeEntry.setFseq((long) i+1);
				makeupChargeEntryMapper.insertSelective(makeupChargeEntry);
			}
		}
	}

	/**
	 * 实现审核
	 */
	@Override
	public int auditMakeupCharge(MakeupCharge makeupCharge) {
		// TODO Auto-generated method stub
		return MakeupChargeMapper.updateByPrimaryKeySelective(makeupCharge);
	}
}
